package com.swotxu.ribboninvoker.ribbon;

import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @Date: 2020/9/19 19:31
 * @Author: swotXu
 */
@Slf4j
public class MyRule implements IRule {
    private ILoadBalancer lb;

    @Override
    public Server choose(Object o) {
        List<Server> servers = lb.getAllServers();
        log.info("这是自定义服务器规则类，输出服务器信息：");
        for (Server s : servers) {
            log.info("-> {}", s.getHostPort());
        }
        log.info("最后选择的服务器为：");
        return servers.get(0);
    }

    @Override
    public void setLoadBalancer(ILoadBalancer iLoadBalancer) {
        this.lb = iLoadBalancer;
    }

    @Override
    public ILoadBalancer getLoadBalancer() {
        return lb;
    }
}
